package net.system.sys.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.core.base.struts.BaseAction;
import net.core.common.HttpUtil;
import net.core.common.StrUtils;
import net.system.base.bean.SSysnotice;
import net.system.base.bean.SSysuser;
import net.system.common.HttpUtils;
import net.system.sys.service.INoticeService;
import net.system.sys.service.IUserService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

@Scope("request")
@Namespace("/system/notice")
@InterceptorRefs(@InterceptorRef("basicStack"))
@ParentPackage("struts-default")
@Results({ @Result(name = "error", location = "/login.jsp") })
public class NoticeAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	@Autowired
	INoticeService service;
	private List<SSysnotice> list;

	private String recId;

	private SSysnotice bean;
	
	private Map<String,String> mapReturn;

	private String reCode = "";

	@Action(value = "list", results = { @Result(name = "success", location = "/WEB-INF/pages/system/NoticeList.jsp") })
	public String list() {
		HashMap map = HttpUtil.buildParameterMap(getRequest());
		String funid = StrUtils.getNotNullStrValue(map.get("funid"));
		mapReturn = new HashMap();
		mapReturn.put("funid", funid);
		if("notice".equals(funid)){
			mapReturn.put("title", "公告管理");
		}else if("help".equals(funid)){
			mapReturn.put("title", "帮助管理");
		}
		// 管理界面包含查询及列表内容
		list = service.select(map);
		return SUCCESS;
	}

	@Action(value = "add", results = { @Result(name = "success", location = "/WEB-INF/pages/system/NoticeEdt.jsp") })
	public String add() {
		// 添加页面
		HashMap map = HttpUtil.buildParameterMap(getRequest());
		String funid = StrUtils.getNotNullStrValue(map.get("funid"));
		mapReturn = new HashMap();
		mapReturn.put("funid", funid);
		if("notice".equals(funid)){
			mapReturn.put("title", "公告管理");
		}else if("help".equals(funid)){
			mapReturn.put("title", "帮助管理");
		}
		String act = StrUtils.getNotNullStrValue(map.get("act"));
		if("add".equals(act)){
			mapReturn.put("act", "添加");
		}else if("edit".equals(act)){
			mapReturn.put("act", "修改");
		}
		
		//String formFlag = StrUtils.getNotNullStrValue(map.get("formFlag"));
		//mapReturn.put("formFlag", formFlag);
		
		if (null == recId || "".equals(recId)) {
			recId = HttpUtils.getCurrentLoginUserId(getRequest());
			
		}
		
		if(!"".equals(recId)){
			bean = service.getBean(recId);
		}
		
		return SUCCESS;
	}


	/**
	 * @return
	 */
	@Action(value = "save", results = { @Result(name = "success", location = "/WEB-INF/pages/system/NoticeEdt.jsp") })
	public String save() {
		// 保存信息
		// 保存数据
		HashMap map = HttpUtil.buildParameterMap(getRequest());
		mapReturn = new HashMap();
		//String formFlag = StrUtils.getNotNullStrValue(map.get("formFlag"));
		mapReturn.put("formFlag", "1");
		String funid = StrUtils.getNotNullStrValue(map.get("funid"));
		mapReturn.put("funid", funid);
		System.out.println(map.toString());
		// 如果 recId为空。则增加操作
		if (null == recId || "".equals(recId)) {
			service.insert(map);
		} else {
			service.update(map);
		}
		bean = service.getBean(recId);
		return SUCCESS;
	}

	@Action(value = "show", results = { @Result(name = "success", location = "/WEB-INF/pages/system/NoticeEdt.jsp") })
	public String show() {
		// 显示详细页面
		bean = service.getBean(recId);
		return SUCCESS;
	}

	public String getRecId() {
		return recId;
	}

	public void setRecId(String recId) {
		this.recId = recId;
	}

	public SSysnotice getBean() {
		return bean;
	}

	public void setBean(SSysnotice bean) {
		this.bean = bean;
	}

	public void setList(List<SSysnotice> list) {
		this.list = list;
	}


	public String getReCode() {
		return reCode;
	}

	public void setReCode(String reCode) {
		this.reCode = reCode;
	}

	public List<SSysnotice> getList() {
		return list;
	}

	public Map<String, String> getMapReturn() {
		return mapReturn;
	}

	public void setMapReturn(Map<String, String> mapReturn) {
		this.mapReturn = mapReturn;
	}

}
